package com.example.schoolnewsapp;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.schoolnewsapp.JavaBean.news_info;
import com.example.schoolnewsapp.database.DBHelper;
import com.example.schoolnewsapp.databinding.ActivitySingleNewsBinding;


public class SingleNewsActivity extends AppCompatActivity {
    private ImageView fav_news;
    private ImageView dianzan_news;
    private Button comment;
    private EditText context;
    private DBHelper dbHelper;
    private ActivitySingleNewsBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        binding = ActivitySingleNewsBinding.inflate(getLayoutInflater());
        super.onCreate(savedInstanceState);
        setContentView(binding.getRoot());
        initView();
        dbHelper = new DBHelper(this, "UserDB.db", null, 6);

        final Intent intent = getIntent();
        final String news_url = intent.getStringExtra("url");
        final String news_title = intent.getStringExtra("title");
        final String news_date = intent.getStringExtra("date");
        final String news_author = intent.getStringExtra("author");
        final String news_picurl = intent.getStringExtra("pic_url");


        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Dao daohelper = new Dao();
                String text = context.getText().toString();

//                add_comment(text,news_url,news_title,news_date,news_author,news_picurl);
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("news_url", news_url);
                values.put("news_title", news_title);
                values.put("news_date", news_date);
                values.put("news_author", news_author);
                values.put("news_picurl", news_picurl);
                values.put("context",text);

                db.insert("Comment",null,values);
                db.close();

                Toast.makeText(SingleNewsActivity.this,"评论成功",Toast.LENGTH_LONG).show();
                context.setText("");
            }
        });

        fav_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                fav_news.setImageResource(R.drawable.favorite_selected);


                SQLiteDatabase db = dbHelper.getWritableDatabase();

                ContentValues values = new ContentValues();
                //组装数据
                values.put("news_url", news_url);
                values.put("news_title", news_title);
                values.put("news_date", news_date);
                values.put("news_author", news_author);
                values.put("news_picurl", news_picurl);
              //  db.insert("Fav_News", null, values);
                db.close();
                Toast.makeText(SingleNewsActivity.this, "收藏成功！", Toast.LENGTH_SHORT).show();
            }
        });


        dianzan_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dianzan_news.setImageResource(R.drawable.dianzan_choose);

//                int channel = query_channel(news_title);
//                update_good(channel,news_title);
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("news_url", news_url);
                values.put("news_title", news_title);
                values.put("news_date", news_date);
                values.put("news_author", news_author);
                values.put("news_picurl", news_picurl);

              //  db.insert("dianzan_News",null,values);
                db.close();

                Toast.makeText(SingleNewsActivity.this,"点赞成功",Toast.LENGTH_LONG).show();
            }
        });


        news_info mediafocus = (news_info) intent.getSerializableExtra("news_info");
        binding.meMain.setText(mediafocus.getContext());
        binding.meTime.setText(mediafocus.getCreatedAt());
        binding.meTitle.setText(mediafocus.getTitle());
        binding.img.setOnClickListener(v -> {
            Intent intent1 = new Intent(SingleNewsActivity.this, MainActivity.class);
            startActivity(intent1);
        });
    }



    private void initView() {

        fav_news =(ImageView) findViewById(R.id.fav_news);
        dianzan_news = (ImageView) findViewById(R.id.dianzan_news);
        comment = (Button) findViewById(R.id.btncomment);
        context = (EditText) findViewById(R.id.comment);
    }
}